package com.media.themovieworld.screen.main;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.media.themovieworld.R;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.api.RequestModel;
import com.media.themovieworld.common.DataStoreManage;
import com.media.themovieworld.common.adapter.SmartFragmentStatePagerAdapter;
import com.media.themovieworld.common.fragment.BaseFragmentScreen;
import com.media.themovieworld.common.fragment.MovieTaskScreen;
import com.media.themovieworld.model.AdvertiseModel;
import com.media.themovieworld.model.ObServerListener;
import com.media.themovieworld.model.PageItemInfo;
import com.media.themovieworld.model.RegisterResultModel;
import com.media.themovieworld.widget.viewpagerindicator.TitlePageIndicator;

/**
 * The Class MainScreen.
 */
public class MainScreen extends MovieTaskScreen{
	
	/** The m adapter. */
	private MainViewPageAdapter mAdapter;
	
	/** The m pager. */
	private ViewPager mPager;
	
	/** The m indicator. */
	private TitlePageIndicator mIndicator;
	
	/** The m list page info. */
	public ArrayList<PageItemInfo> mListPageInfo;
	
	private View root; 
	/* (non-Javadoc)
	 * @see com.media.themovieworld.screen.common.BaseFragmentScreen#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		root = inflater.inflate(R.layout.tmw__home_screen, null);
//        registDevice();
		getListAdvertise();
		return root;
	}
	
	public ViewPager getViewPager(){
		return mPager;
	}
	
	
	/**
	 * Creates the view.
	 *
	 * @param v the v
	 */
	private void createView(){
		loadListPageInfo();
		mAdapter = new MainViewPageAdapter(getFragmentManager());

        mPager = (ViewPager)root.findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(mListPageInfo.size());
        mPager.setAdapter(mAdapter);
        mIndicator = (TitlePageIndicator)root.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        mPager.setCurrentItem(1);
	}
	
	public void update() {
		if (mAdapter == null) return;
		mAdapter.notifyDataSetChanged();
	}
	
	/**
	 * Regist device.
	 *
	 * @param v the v
	 */
	public void registDevice(){
//		final SoapRequestModel requestData = MovieWorldApi.getDeviceConnectedRequest(getActivity());
		final RequestModel requestData = ApiParams.getdeviceConnectParams(getActivity(), "");
		executeSoapTask(requestData, true, RegisterResultModel.class, new MovieTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
//				if (object!=null) {
					getListAdvertise();
//				}
			}
		});
	}
	
	public void getListAdvertise(){
		final RequestModel requestModel = ApiParams.getAdvertiseParams();
		executeSoapTaskReturnList(requestModel, true, AdvertiseModel.class, new MovieTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if(object!=null){
					List<AdvertiseModel> listAdvertise = (List<AdvertiseModel>) object;
					DataStoreManage.getInstance().setListAdvertise(listAdvertise);
				}
				createView();
			}
		});
	}
	/**
	 * Load list page info.
	 */
	private void loadListPageInfo(){
		mListPageInfo = new ArrayList<PageItemInfo>();
		if(getActivity() == null) return;
		
		PageItemInfo categoryPage = new PageItemInfo();
		categoryPage.pageTitle = getResources().getString(R.string.category);
		categoryPage.fragClass = CategoryScreen.class;
		
		PageItemInfo suggestPage = new PageItemInfo();
		suggestPage.pageTitle = getResources().getString(R.string.suggest_movies);
		suggestPage.fragClass = SuggestMovieScreen.class;
		
		PageItemInfo spotlightPage = new PageItemInfo();
		spotlightPage.pageTitle = getResources().getString(R.string.tmw__home_activity_tab_suggest_2);
		spotlightPage.fragClass = SpotLightScreen.class;
		
		PageItemInfo hotmoviePage = new PageItemInfo();
		hotmoviePage.pageTitle = getResources().getString(R.string.hot_movies);
		hotmoviePage.fragClass = HotMovieScreen.class;
		
		PageItemInfo newmoviePage = new PageItemInfo();
		newmoviePage.pageTitle = getResources().getString(R.string.newfilms);
		newmoviePage.fragClass = NewMovieScreen.class;
		
		mListPageInfo.add(categoryPage);
		mListPageInfo.add(suggestPage);
		mListPageInfo.add(spotlightPage);
		mListPageInfo.add(hotmoviePage);
		mListPageInfo.add(newmoviePage);
	}
	
	
	/**
	 * The Class MainViewPageAdapter.
	 */
	class MainViewPageAdapter extends SmartFragmentStatePagerAdapter{

	    /**
    	 * Instantiates a new main view page adapter.
    	 *
    	 * @param fm the fm
    	 */
    	public MainViewPageAdapter(FragmentManager fm) {
	        super(fm);
	    }
    	
	    /* (non-Javadoc)
    	 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
    	 */
    	@Override
	    public Fragment getItem(int position) {
    		BaseFragmentScreen frag = null;
			try {
				frag = (BaseFragmentScreen) mListPageInfo.get(position).fragClass.newInstance();
				frag.setPageTitle(mListPageInfo.get(position).pageTitle);
//				if (frag instanceof CategoryScreen) {
//					((CategoryScreen)frag).registerObserver();
//				}
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}catch (Exception e) {
				e.printStackTrace();
			}
	        return frag;
	    }

	    /* (non-Javadoc)
    	 * @see android.support.v4.view.PagerAdapter#getCount()
    	 */
    	@Override
	    public int getCount() {
	        return mListPageInfo.size();
	    }

	    /* (non-Javadoc)
    	 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
    	 */
    	@Override
	    public CharSequence getPageTitle(int position) {
	      return mListPageInfo.get(position).pageTitle;
	    }
    	
    	@Override
    	public int getItemPosition(Object item) {
    		if(!(item instanceof BaseFragmentScreen) || item == null) return POSITION_NONE;
            String title = ((BaseFragmentScreen)item).getPageTitle();
            int position = -1;
            for (int i = 0; i < mListPageInfo.size(); i++) {
				if(mListPageInfo.get(i).pageTitle.equals(title)) {
					position = i;
					break;
				}
			}
            if (position >= 0) {
            	if(item instanceof ObServerListener){
        			((ObServerListener)item).update(null);
        		}
                return position;
            } else {
                return POSITION_NONE;
            }
    	}
	}
}
